<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 水上铁 <wxm201411@163.com>
// +----------------------------------------------------------------------
//

class MailAction extends Action{
	
	function _initialize(){		
		$this->setTitle( '发送邮件' );
	}	

	function index(){
		$this->display();
	}
	
	function doSend(){
		if( $this->_lock() ){
			$this->error('操作频繁，请在3分钟后再操作.');
		}
		
		$email   = t($_POST['email']);
		if(empty($email)){
			$this->error('邮件地址出错.');
		}
		$title   = t($_POST['title']);
		$content = t($_POST['content']);
		if(empty($title) || empty($content)){
			$this->error('标题或内容不能为空.');
		}			
		
		$user = D('User')->where('uid='.$this->mid)->field('fullname,uname')->find();
		$content .= '<br/>来自：'.$user['fullname'].'( '.$user['uname'].' )'; 

		$res    = Service('Mail')->send($email, $title, $content);
		if($res){
			$this->success('发送成功.');
		}else{
			$this->error('发送失败.');
		}
	}
	
	//限制5分钟内不能重复发邮件，以防止恶意攻击
	function _lock(){
		$uid = $this->mid;
		if(!isset($_SESSION['email_lock_'.$uid])){
			$_SESSION['email_lock_'.$uid] = time();
			return false;
		}		

		$lock_time = $_SESSION['email_lock_'.$uid]+180;
		$now_time = time();	
			
		if($lock_time>$now_time){
			return true;
		}else{
			$_SESSION['email_lock_'.$uid] = time();
			return false;
		}
	}
}
?>